//
//  CNHelpPlistController.m
//  test2
//
//  Created by 刘家强 on 16/9/27.
//  Copyright © 2016年 CN Team. All rights reserved.
//

#import "CNHelpPlistController.h"
#import "CNHelpPlistModel.h"
#import "CNHelpPlistHeaderView.h"
#import "CNHelpPlistCell.h"

@interface CNHelpPlistController ()

@property (nonatomic, strong) NSArray <CNHelpPlistModel *>*plistArr;

@end

@implementation CNHelpPlistController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self loadPlist];
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight = 60;
    self.title = @"常见问题";
    
}

- (void)loadPlist {
    NSArray *tempArr = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"HelpPlist.plist" ofType:nil]];
    NSMutableArray *arr = [NSMutableArray array];
    
    for (NSDictionary *dict in tempArr) {
        CNHelpPlistModel *model = [CNHelpPlistModel helpPlistWithDict:dict];
        [arr addObject:model];
    }
    self.plistArr = arr.copy;
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.plistArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.plistArr[section].count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    static NSString *ID = @"HelpCell";
    
    CNHelpPlistCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    if (cell == nil) {
        cell = [[CNHelpPlistCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
    }
    
    NSArray *arr = self.plistArr[indexPath.section].texts;
    cell.label.text = arr[indexPath.row];
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    CNHelpPlistHeaderView *view = [CNHelpPlistHeaderView helpPlistHeaderView];
    view.titleLabe.text = self.plistArr[section].title;
    
    view.but.tag = section;
    [view.but addTarget:self action:@selector(butClick:) forControlEvents:UIControlEventTouchUpInside];
    
    view.but.selected = self.plistArr[section].isSelected;
    
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 44;
}
- (void)butClick:(UIButton *)but {
    
    self.plistArr[but.tag].isSelected = !self.plistArr[but.tag].isSelected;
    [self tableView:self.tableView heightForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
  
    if (!self.plistArr[but.tag].count) {
        self.plistArr[but.tag].count = self.plistArr[but.tag].texts.count;
    } else {
        self.plistArr[but.tag].count = 0;
    }
    
    NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:but.tag];
    [self.tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];
}

@end
